package com.atxiaodei.springbootkabudazkapi.zookeepercrud;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.data.Stat;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName UpdateZookeeperDemo
 * @Deacription TODO
 * @Author 卡布达
 * @Date 2022/1/16 22:02
 * @Version 1.0
 **/
public class UpdateZookeeperDemo {

    public void update() throws Exception {
        CuratorFramework client= CuratorFrameworkFactory
                .builder()
                .connectionTimeoutMs(50000)
                .connectString("192.168.0.108:2181") //读写分离(zookeeper-server)
                // baseSleepTimeMs*Math.max(1,random.nextInt(1<<(maxRetries+1))
                /**
                 * RetryNTimes 指定最大重试次数
                 * RetryOneTimes
                 * RetryUntilElapsed 一直重试，直到达到规定时间
                 */
                .retryPolicy(new ExponentialBackoffRetry(1000,3))
                .sessionTimeoutMs(15000)
                // 名称空间 每次操作zk /kabuda
                .namespace("kabuda")
                .build();
        client.start(); //启动
        // 基本修改
        client.setData().forPath("/app1", "卡布达".getBytes());
        System.out.println("------------修改成功---------------");
        TimeUnit.SECONDS.sleep(10);
        // 根据版本修改
        Stat stat = new Stat();
        client.getData().storingStatIn(stat).forPath("/app1");
        int version = stat.getVersion();
        client.setData().withVersion(version).forPath("/app1", "诸葛孔暗".getBytes());
        System.out.println("根据版本修改成功,当前版本--->" + stat.getVersion());
    }

    public static void main(String[] args) throws Exception {
        UpdateZookeeperDemo updateZookeeperDemo=new UpdateZookeeperDemo();
        updateZookeeperDemo.update();
    }
}
